package com.bestv.search.common.dao.hibernate;

import com.bestv.search.common.dao.TopKeywordDao;
import com.bestv.search.common.model.TopKeyword;
import org.hibernate.Query;
import org.hibernate.Session;

import java.util.List;

/**
 * @author Michael Wang
 */
public class TopKeywordDaoHibernate extends GenericDaoHibernate<TopKeyword, Long> implements TopKeywordDao {

    public TopKeywordDaoHibernate() {
        super(TopKeyword.class);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<TopKeyword> getTopKeywordByKeyword(String keyword) {
        return getHibernateTemplate().find("from TopKeyword where keyword = ?", keyword);
    }

    @Override
    public void saveTopKeywords(List<TopKeyword> storeList) {
        getHibernateTemplate().saveOrUpdateAll(storeList);
    }

    @Override
    public void removeTopKeyword(String keyword) {
        List<TopKeyword> deleteKeywords = getTopKeywordByKeyword(keyword);
        if (deleteKeywords != null)
            for (TopKeyword deleteKeyword : deleteKeywords)
                getHibernateTemplate().delete(deleteKeyword);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<TopKeyword> getTopKeywordByKeyword(String keyword, int start, int limit) {
        String sql = "from TopKeyword topkeyword where topkeyword.keyword like '%" + keyword + "%' order by topkeyword.count desc";
        Session session = null;
        try {
            session = getSession();
            Query query = session.createQuery(sql);
            query.setFirstResult(start);
            query.setMaxResults(limit);

            return query.list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null)
                releaseSession(session);
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<TopKeyword> getTopKeywords(int start, int limit) {
        String sql = "from TopKeyword topkeyword order by topkeyword.count desc";
        Session session = null;
        try {
            session = getSession();
            Query query = session.createQuery(sql);
            query.setFirstResult(start);
            query.setMaxResults(limit);

            return query.list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null)
                releaseSession(session);
        }
        return null;
    }

    @Override
    public int getTotalCount() {
        String sql = "select count(id) from TopKeyword";
        Session session = null;
        try {
            session = getSession();
            Query query = session.createQuery(sql);
            Object obj = query.uniqueResult();
            if (obj != null) {
                return Integer.valueOf(obj.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null)
                releaseSession(session);
        }
        return 0;
    }

    @Override
    public int getTotalCountByKeyword(String keyword) {
        String sql = "select count(id) from TopKeyword where keyword like '%" + keyword + "%'";
        Session session = null;
        try {
            session = getSession();
            Query query = session.createQuery(sql);
            Object obj = query.uniqueResult();
            if (obj != null) {
                return Integer.valueOf(obj.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null)
                releaseSession(session);
        }
        return 0;
    }
}
